package com.itsc.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.itsc.entities.Role;
import com.itsc.entities.User;
import com.itsc.service.inter.IRoleService;
import com.itsc.service.inter.IUserService;
import com.itsc.util.HttpUtil;
import com.itsc.util.MD5Util;
import com.itsc.util.page.Page;
import com.itsc.util.page.PageUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction implements ModelDriven<User> {

	private IUserService userService;
	private IRoleService roleService;

	private List<Integer> check_id;

	private String datemin;// 起始时间
	private String datemax;// 结束时间
	private String searchname;// 查询条件

	/* 分页显示数据 */
	private int curPage = 1;// 当前页
	private int pageSize = 8;// 每页显示条数
	private int countPage;// 总页数

	private User model = new User();

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	public List<Integer> getCheck_id() {
		return check_id;
	}

	public void setCheck_id(List<Integer> check_id) {
		this.check_id = check_id;
	}

	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	private Integer role_id;

	public Integer getRole_id() {
		return role_id;
	}

	public void setRole_id(Integer role_id) {
		this.role_id = role_id;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCountPage() {
		return countPage;
	}

	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}

	public String getDatemin() {
		return datemin;
	}

	public void setDatemin(String datemin) {
		this.datemin = datemin;
	}

	public String getDatemax() {
		return datemax;
	}

	public void setDatemax(String datemax) {
		this.datemax = datemax;
	}

	public String getSearchname() {
		return searchname;
	}

	public void setSearchname(String searchname) {
		this.searchname = searchname;
	}
	
	public String list() throws Exception {

		int count = userService.getCount();
		PageUtils.initParamter(count, pageSize);
		Page page = PageUtils.getPage();
		List<User> userList = userService.getPage(page);
		ActionContext.getContext().put("userList", userList);
		ActionContext.getContext().put("page", page);
		return "list";
	}

	public String show() throws Exception {
		User user = userService.selectById(model.getUser_id());
		ActionContext.getContext().getValueStack().push(user);
		return "show";
	}

	public String add() throws Exception {

		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String time = format.format(date);
		Date datetime = format.parse(time);

		Set<Role> roleSet = new HashSet<Role>();
		Role role = roleService.selectById(role_id);
		roleSet.add(role);

		model.setUser_pwd(MD5Util.getStringMD5(model.getUser_pwd()));

		model.setUser_regtime(datetime);
		model.setRoleSet(roleSet);
		userService.save(model);
		return "ok";
	}

	public String addModel() {
		List<Role> roleList = roleService.selectAll();
		ActionContext.getContext().put("roleList", roleList);
		return "insert";
	}

	/**
	 * 验证用户名是否唯一
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkUser() throws Exception {

		Boolean flag = userService.selectByString(model.getUser_name());
		Integer id = userService.selectIdByString(model.getUser_name());
		
		
		if (flag) {

			ServletActionContext.getResponse().getWriter().print("1");
		} else {
			if(id==model.getUser_id()){
				ServletActionContext.getResponse().getWriter().print("1");
			}else{
				ServletActionContext.getResponse().getWriter().print("0");
			}
		}

		return null;
	}
	
	public String checkUserName() throws Exception{
		
		Boolean flag = userService.selectByString(model.getUser_name());
		
		if (flag) {
			ServletActionContext.getResponse().getWriter().print("1");
		} else {
			
			ServletActionContext.getResponse().getWriter().print("0");
		}

		return null;
	}

	/**
	 * 重置密码为000000
	 * 
	 * @throws Exception
	 */
	public String resetpwd() throws Exception {

		User user = userService.selectById(model.getUser_id());
		user.setUser_pwd(MD5Util.getStringMD5("000000"));
		userService.update(user);

		return "ok";
	}

	public String edit() throws Exception {

		List<Role> roleList = roleService.selectAll();
		ActionContext.getContext().put("roleList", roleList);

		User user = userService.selectById(model.getUser_id());
		ActionContext.getContext().getValueStack().push(user);
		return "edit";

	}
	public String update() throws Exception{
		
		Set<Role> roleSet = new HashSet<Role>();
		Role role = roleService.selectById(role_id);
		roleSet.add(role);
		
		model.setRoleSet(roleSet);
		
		userService.update(model);
		
		return "ok";
	}
	public String delete() throws Exception {
		User user = userService.selectById(model.getUser_id());
		userService.delete(user);
		return "ok";
	}

	/**
	 * 批量删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String moredelete() throws Exception {

		for (Integer id : check_id) {
			User user = userService.selectById(id);
			userService.delete(user);
		}
		return "ok";
	}

	/**
	 * 搜索分页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		List<User> userList = null;
		Page page = null;
		if ((searchname == null || searchname.equals("")) && (datemin == null || datemin.equals(""))
				&& (datemax == null || datemax.equals(""))) {
			int count = userService.getCount();
			PageUtils.initParamter(count, pageSize);
			page = PageUtils.getPage();
			if (curPage <= 0) {
				curPage = 1;
			} else if (curPage > page.getTotalPage()) {
				curPage = page.getTotalPage();
			} else {
			}
			PageUtils.setCurrentPage(curPage);
			page = PageUtils.getPage();
			userList = userService.search(searchname, datemin, datemax,
					page);
		} else {
			int count = userService.getCount(searchname, datemin, datemax);
			PageUtils.initParamter(count, pageSize);
			PageUtils.setCurrentPage(curPage);
			page = PageUtils.getPage();
			userList = userService.search(searchname, datemin, datemax,
					page);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", userList);
		map.put("page", page);
		String js = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
		HttpUtil.rendText(js);
		return null;
	}
	
	public String checkUserPhone() throws Exception{
		Boolean flag = userService.checkUserPhone(model.getUser_phone());
		
		if (flag) {
			ServletActionContext.getResponse().getWriter().print("1");
		} else {
			
			ServletActionContext.getResponse().getWriter().print("0");
		}

		return null;
	}
	
	public String checkPhone() throws Exception{
		Boolean flag = userService.checkUserPhone(model.getUser_phone());
		Integer id = userService.selectIdByPhone(model.getUser_phone());
		
		if (flag) {

			ServletActionContext.getResponse().getWriter().print("1");
		} else {
			if(id==model.getUser_id()){
				ServletActionContext.getResponse().getWriter().print("1");
			}else{
				ServletActionContext.getResponse().getWriter().print("0");
			}
		}

		return null;
	}
	
	public String checkUserEmail() throws Exception{
		Boolean flag = userService.checkUserEmail(model.getUser_email());
		
		if (flag) {
			ServletActionContext.getResponse().getWriter().print("1");
		} else {
			
			ServletActionContext.getResponse().getWriter().print("0");
		}

		return null;
	}
	
	public String checkEmail() throws Exception{
		Boolean flag = userService.checkUserEmail(model.getUser_email());
		Integer id = userService.selectIdByEmail(model.getUser_email());
		
		if (flag) {

			ServletActionContext.getResponse().getWriter().print("1");
		} else {
			if(id==model.getUser_id()){
				ServletActionContext.getResponse().getWriter().print("1");
			}else{
				ServletActionContext.getResponse().getWriter().print("0");
			}
		}

		return null;
	}

}
